#pragma once

#include <string>
#include <strstream> 

#include <sstream>

using namespace std;

#ifdef UNICODE 

    #ifndef TSTRING

			#define  TSTRING

			typedef   basic_string<WCHAR>    tstring;
			
			typedef   wstringstream     MyStream;	
    #endif

#else
    typedef   strstream     MyStream;	
	typedef   string        tstring ;

#endif

#define  IS_FIND(str)      (str.compare(_T("not find")) != 0)    

class CIniFile
{
public:
	CIniFile(TCHAR* pStrIniPath){m_strIniFilePath = tstring(pStrIniPath); };
	CIniFile(tstring strIniPath){m_strIniFilePath = strIniPath; };
	~CIniFile(void){};
	CIniFile(){};


private: 
	   tstring     m_strIniFilePath;

public:

    template<typename T> 
	BOOL  Get(TCHAR * pAppName,TCHAR* pKeyName, T& tValue  ){}
	
	template<>
	BOOL  Get<int>(TCHAR * pAppName,TCHAR* pKeyName, int &tValue)
	{
		if (pAppName == NULL || pKeyName == NULL || m_strIniFilePath.empty())
		{
		   return FALSE ;
		}

		tValue = ::GetPrivateProfileInt(pAppName,pKeyName,0,m_strIniFilePath.c_str());
		
		return TRUE;
	}
    template<>
	BOOL  Get<long>(TCHAR * pAppName,TCHAR* pKeyName, long &tValue)
	{
		if (pAppName == NULL || pKeyName == NULL || m_strIniFilePath.empty())
		{
			return FALSE ;
		}

		tValue = ::GetPrivateProfileInt(pAppName,pKeyName,0,m_strIniFilePath.c_str());
		return TRUE;


	}

    template<>
	BOOL  Get<tstring>(TCHAR * pAppName,TCHAR* pKeyName, tstring& tValue)
	{
		if (pAppName == NULL || pKeyName == NULL || m_strIniFilePath.empty())
		{
			return FALSE ;
		}
		TCHAR   strDefault[]=_T("not find");
		TCHAR	strReturn[MAX_PATH] ={0};
		DWORD dwRet = ::GetPrivateProfileString(pAppName,pKeyName,strDefault,strReturn,MAX_PATH,m_strIniFilePath.c_str());
        
		if (dwRet >= 0)
		{
		    tValue = strReturn;
		}

		return dwRet >= 0;
	}

	template<typename T> 
	BOOL  Set(TCHAR * pAppName,TCHAR* pKeyName, T tValue  ){}

	template<>
	BOOL  Set<int>(TCHAR * pAppName,TCHAR* pKeyName, int tValue)
	{
		if (pAppName == NULL || pKeyName == NULL || m_strIniFilePath.empty())
		{
			return FALSE ;
		}
        MyStream  strWirte ;
		strWirte <<tValue ;

	    return ::WritePrivateProfileString(pAppName,pKeyName,strWirte.str().c_str(),m_strIniFilePath.c_str());

	}
	template<>
	BOOL  Set<long>(TCHAR * pAppName,TCHAR* pKeyName, long tValue)
	{
		if (pAppName == NULL || pKeyName == NULL || m_strIniFilePath.empty())
		{
			return FALSE ;
		}

		MyStream  strWirte ;
		strWirte <<tValue ;
		return ::WritePrivateProfileString(pAppName,pKeyName,strWirte.str().c_str(),m_strIniFilePath.c_str());

	}
	template<>
	BOOL  Set<tstring>(TCHAR * pAppName,TCHAR* pKeyName, tstring tValue)
	{
		if (pAppName == NULL || pKeyName == NULL || m_strIniFilePath.empty())
		{
			return FALSE ;
		}
	
		return ::WritePrivateProfileString(pAppName,pKeyName,tValue.c_str(),m_strIniFilePath.c_str());

	}



	void SetPath(tstring strPath)
	{
	   m_strIniFilePath = strPath ;
	}
};
